import com.alibaba.fastjson.JSON;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class SchoolServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Integer pid = Integer.valueOf(req.getParameter("pid"));
        resp.setContentType("application/json;charset=utf-8");
        try {
            Connection con = DruidPool.getCon();
            PreparedStatement ps = con.prepareStatement("select * from university where pid=?");
            ps.setInt(1, pid);
            ResultSet rs = ps.executeQuery();
            List<SchoolBean> univers = new ArrayList();
            while (rs.next()) {
                SchoolBean school = new SchoolBean();
                school.setId(rs.getInt("uid"));
                school.setName(rs.getString("uname"));
                univers.add(school);
            }
            String json = JSON.toJSONString(univers);
            resp.getWriter().write(json);
            DruidPool.close(rs,null,ps,con);
        } catch (Exception e) {
            e.printStackTrace();
        }


    }
}

